home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / gsstate.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  13KB  |  448 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsstate.c */
  20. /* Miscellaneous graphics state operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gserrors.h"
  24. #include "gxrefct.h"            /* early for gscie.h */
  25. #include "gxcolor.h"            /* for gscolor2.h, gscie.h */
  26. #include "gzstate.h"
  27. #include "gzdevice.h"
  28. #include "gscspace.h"
  29. #include "gscolor2.h"
  30. #include "gscie.h"
  31. #include "gzcolor.h"            /* requires gxdevice.h */
  32. #include "gzht.h"
  33. #include "gzline.h"
  34. #include "gzpath.h"
  35.  
  36. /* Imported values */
  37. /* The following should include a 'const', but */
  38. /* the Watcom compiler won't accept it. */
  39. extern /*const*/ gx_color_map_procs *cmap_procs_default;
  40.  
  41. /* Forward references */
  42. private gs_state *alloc_gstate(P3(const gs_memory_procs *, const gs_state *,
  43.   const char *));
  44. private int alloc_gstate_contents(P1(gs_state *));
  45. private void free_gstate_contents(P1(gs_state *));
  46. private void copy_gstate_contents(P2(gs_state *pto, const gs_state *pfrom));
  47.  
  48. /* The structure for allocating (most of) the contents of a gstate */
  49. /* all at once. */
  50. typedef struct gs_state_contents_s {
  51.     gx_path path;
  52.     gx_clip_path clip_path;
  53.     line_params line_params;
  54.     halftone_params halftone;
  55.     gs_color_space color_space;
  56.     gs_client_color ccolor;
  57.     gx_device_color dev_color;
  58. } gs_state_contents;
  59. #define gstate_contents(pgs) ((gs_state_contents *)((pgs)->path))
  60.  
  61. /* Allocate and free a structure */
  62. #define alloc_struct(pgs,typ,cname)\
  63.   (typ *)(*(pgs)->memory_procs->alloc)(1, sizeof(typ), cname)
  64. #define free_struct(pgs,ptr,cname)\
  65.   (*(pgs)->memory_procs->free)((char *)(ptr), 1, sizeof(*(ptr)), cname)
  66.  
  67. /* ------ Operations on the entire graphics state ------ */
  68.  
  69. /* Allocate and initialize a graphics state. */
  70. private float
  71. null_transfer(const gs_state *pgs, floatp gray)
  72. {    return gray;
  73. }
  74. gs_state *
  75. gs_state_alloc(const gs_memory_procs *mprocs)
  76. {    register gs_state *pgs =
  77.         alloc_gstate(mprocs, (gs_state *)0, "gs_state_alloc");
  78.     if ( pgs == 0 ) return 0;
  79.     pgs->saved = 0;
  80.     /* Initialize things not covered by initgraphics */
  81.     gx_path_init(pgs->path, pgs->memory_procs);
  82.     gx_cpath_init(pgs->clip_path, pgs->memory_procs);
  83.     gx_alloc_ht_cache(pgs);
  84.     pgs->halftone->width = pgs->halftone->height =
  85.         pgs->halftone->order_size = 0;
  86.     gs_sethalftonephase(pgs, 0, 0);
  87.     /* Initialize things so that gx_remap_color won't crash. */
  88.     gx_set_black(pgs);
  89.     pgs->overprint = 0;
  90.     pgs->black_generation = 0;
  91.     pgs->undercolor_removal = 0;
  92.     pgs->cmap_procs = cmap_procs_default;
  93.     {    gx_transfer *ptran = &pgs->transfer;
  94.         rc_alloc_struct_n(ptran->gray, gx_transfer_map, mprocs,
  95.                   return 0, "gs_state_alloc", 4);
  96.         ptran->red = ptran->green = ptran->blue = ptran->gray;
  97.         ptran->gray->proc = null_transfer;
  98.         ptran->gray->values[0] = frac_0;
  99.     }
  100.     gs_nulldevice(pgs);
  101.     gs_settransfer(pgs, null_transfer);
  102.     gs_setflat(pgs, 1.0);
  103.     gs_setstrokeadjust(pgs, 1);
  104.     /****** What about the font ? ******/
  105.     pgs->in_cachedevice = pgs->in_charpath = 0;
  106.     pgs->show_gstate = 0;
  107.     pgs->level = 0;
  108.     pgs->fill_adjust = float2fixed(0.25);
  109.     pgs->client_data = 0;
  110.     if ( gs_initgraphics(pgs) < 0 )
  111.        {    /* Something went very wrong */
  112.         return 0;
  113.        }
  114.     return pgs;
  115. }
  116.  
  117. /* Set the client data in a graphics state. */
  118. /* This should only be done to a newly created state. */
  119. void
  120. gs_state_set_client(gs_state *pgs, char/*void*/ *pdata,
  121.   const gs_state_client_procs *pprocs)
  122. {    pgs->client_data = pdata;
  123.     pgs->client_procs = *pprocs;
  124. }
  125.  
  126. /* Get the client data from a graphics state. */
  127. char/*void*/ *
  128. gs_state_client_data(gs_state *pgs)
  129. {    return pgs->client_data;
  130. }
  131.  
  132. /* Free a graphics state */
  133. int
  134. gs_state_free(gs_state *pgs)
  135. {    free_gstate_contents(pgs);
  136.     free_struct(pgs, pgs, "gs_state_free");
  137.     return 0;
  138. }
  139.  
  140. /* Save the graphics state */
  141. int
  142. gs_gsave(gs_state *pgs)
  143. {    gs_state *pnew = alloc_struct(pgs, gs_state, "gs_gsave");
  144.     if ( pnew == 0 )
  145.         return_error(gs_error_VMerror);
  146.     *pnew = *pgs;
  147.     if ( alloc_gstate_contents(pgs) < 0 )
  148.        {    *pgs = *pnew;        /* undo partial alloc */
  149.         free_struct(pgs, pnew, "gs_gsave");
  150.         return_error(gs_error_VMerror);
  151.        }
  152.     copy_gstate_contents(pgs, pnew);
  153.     /* Make pnew, not pgs, have the newly-allocated client data. */
  154.     {    char/*void*/ *pdata = pgs->client_data;
  155.         pgs->client_data = pnew->client_data;
  156.         pnew->client_data = pdata;
  157.     }
  158.     pgs->saved = pnew;
  159.     if ( pgs->show_gstate == pgs )
  160.         pgs->show_gstate = pnew->show_gstate = pnew;
  161.     pgs->level++;
  162.     if_debug2('g', "[g]gsave -> 0x%lx, level = %d\n",
  163.           (ulong)pnew, pgs->level);
  164.     return 0;
  165. }
  166.  
  167. /* Restore the graphics state. */
  168. int
  169. gs_grestore(gs_state *pgs)
  170. {    gs_state *saved = pgs->saved;
  171.     char/*void*/ *pdata = pgs->client_data;
  172.     char/*void*/ *sdata;
  173.     if_debug2('g', "[g]grestore 0x%lx, level was %d\n",
  174.           (ulong)saved, pgs->level);
  175.     if ( !saved ) return gs_gsave(pgs);    /* shouldn't happen */
  176.     sdata = saved->client_data;
  177.     /* Swap back the client data pointers. */
  178.     pgs->client_data = sdata;
  179.     saved->client_data = pdata;
  180.     if ( pdata != 0 && sdata != 0 )
  181.         (*pgs->client_procs.copy)(pdata, sdata);
  182.     free_gstate_contents(pgs);
  183.     *pgs = *saved;
  184.     if ( pgs->show_gstate == saved )
  185.         pgs->show_gstate = pgs;
  186.     free_struct(pgs, saved, "gs_grestore");
  187.     return (pgs->saved == 0 ? gs_gsave(pgs) : 0);
  188. }
  189.  
  190. /* Restore to the bottommost graphics state. */
  191. int
  192. gs_grestoreall(gs_state *pgs)
  193. {    if ( !pgs->saved ) return gs_gsave(pgs);    /* shouldn't happen */
  194.     while ( pgs->saved->saved ) gs_grestore(pgs);
  195.     return gs_grestore(pgs);
  196. }
  197.  
  198. /* Allocate and return a new graphics state. */
  199. gs_state *
  200. gs_gstate(gs_state *pgs)
  201. {    gs_state *pnew = alloc_gstate(pgs->memory_procs, pgs, "gs_gstate");
  202.     if ( pnew == 0 ) return 0;
  203.     copy_gstate_contents(pnew, pgs);
  204.     pnew->saved = 0;
  205.     return pnew;
  206. }
  207.  
  208. /* Copy one previously allocated graphics state to another. */
  209. int
  210. gs_copygstate(gs_state *pto, const gs_state *pfrom)
  211. {    /* This is the same as currentgstate. */
  212.     return gs_currentgstate(pto, pfrom);
  213. }
  214.  
  215. /* Copy the current graphics state to a previously allocated one. */
  216. int
  217. gs_currentgstate(gs_state *pto, const gs_state *pgs)
  218. {    /* We have to copy both the scalar and composite parts */
  219.     /* of the state. */
  220.     gs_state sgs;
  221.     sgs = *pto;
  222.     *pto = *pgs;
  223.     /* Put back the composite part pointers. */
  224. #define gcopy(element)\
  225.     pto->element = sgs.element
  226.     gcopy(path);
  227.     gcopy(clip_path);
  228.     gcopy(line_params);
  229.     gcopy(halftone);
  230.     gcopy(color_space);
  231.     gcopy(ccolor);
  232.     gcopy(dev_color);
  233.     gcopy(cie_render);
  234.     gcopy(transfer.red);
  235.     gcopy(transfer.green);
  236.     gcopy(transfer.blue);
  237.     gcopy(transfer.gray);
  238.     gcopy(device);
  239.     gcopy(client_data);
  240. #undef gcopy
  241.     copy_gstate_contents(pto, pgs);
  242.     return 0;
  243. }
  244.  
  245. /* Restore the current graphics state from a previously allocated one. */
  246. int
  247. gs_setgstate(gs_state *pgs, const gs_state *pfrom)
  248. {    /* The implementation is the same as currentgstate, */
  249.     /* except we must preserve the saved pointer and the level. */
  250.     gs_state *saved = pgs->saved;
  251.     int level = pgs->level;
  252.     int code = gs_currentgstate(pgs, pfrom);
  253.     if ( code < 0 ) return code;
  254.     pgs->saved = saved;
  255.     pgs->level = level;
  256.     return 0;
  257. }
  258.  
  259. /* Swap the saved pointer of the graphics state. */
  260. /* This is provided only for save/restore. */
  261. gs_state *
  262. gs_state_swap_saved(gs_state *pgs, gs_state *new_saved)
  263. {    gs_state *saved = pgs->saved;
  264.     pgs->saved = new_saved;
  265.     return saved;
  266. }
  267.  
  268. /* ------ Operations on components ------ */
  269.  
  270. /* Reset most of the graphics state */
  271. int
  272. gs_initgraphics(register gs_state *pgs)
  273. {    int code;
  274.     gs_initmatrix(pgs);
  275.     if (    (code = gs_newpath(pgs)) < 0 ||
  276.         (code = gs_initclip(pgs)) < 0 ||
  277.         (code = gs_setlinewidth(pgs, 1.0)) < 0 ||
  278.         (code = gs_setlinecap(pgs, gs_cap_butt)) < 0 ||
  279.         (code = gs_setlinejoin(pgs, gs_join_miter)) < 0 ||
  280.         (code = gs_setdash(pgs, (float *)0, 0, 0.0)) < 0 ||
  281.         (code = gs_setgray(pgs, 0.0)) < 0 ||
  282.         (code = gs_setmiterlimit(pgs, 10.0)) < 0
  283.        ) return code;
  284.     return 0;
  285. }
  286.  
  287. /* setflat */
  288. int
  289. gs_setflat(gs_state *pgs, floatp flat)
  290. {    if ( flat <= 0.2 ) flat = 0.2;
  291.     else if ( flat > 100 ) flat = 100;
  292.     pgs->flatness = flat;
  293.     return 0;
  294. }
  295.  
  296. /* currentflat */
  297. float
  298. gs_currentflat(const gs_state *pgs)
  299. {    return pgs->flatness;
  300. }
  301.  
  302. /* setstrokeadjust */
  303. int
  304. gs_setstrokeadjust(gs_state *pgs, int stroke_adjust)
  305. {    pgs->stroke_adjust = stroke_adjust;
  306.     return 0;
  307. }
  308.  
  309. /* currentstrokeadjust */
  310. int
  311. gs_currentstrokeadjust(const gs_state *pgs)
  312. {    return pgs->stroke_adjust;
  313. }
  314.  
  315. /* ------ Internal routines ------ */
  316.  
  317. /* Allocate a graphics state object and its contents, */
  318. /* optionally initializing it from an existing object. */
  319. /* Return 0 if the allocation fails. */
  320. private gs_state *
  321. alloc_gstate(const gs_memory_procs *mprocs, const gs_state *pold,
  322.   const char *cname)
  323. {    gs_state *pgs =
  324.         (gs_state *)(*mprocs->alloc)(1, sizeof(gs_state), cname);
  325.     if ( pgs == 0 ) return 0;
  326.     if ( pold != 0 )
  327.         *pgs = *pold;
  328.     else
  329.         pgs->transfer.red = pgs->transfer.green =
  330.           pgs->transfer.blue = pgs->transfer.gray = 0,
  331.         pgs->cie_render = 0,
  332.         pgs->cie_joint_caches = 0,
  333.         pgs->client_data = 0;
  334.     pgs->memory_procs = mprocs;
  335.     if ( alloc_gstate_contents(pgs) < 0 )
  336.        {    free_struct(pgs, pgs, cname);
  337.         return 0;
  338.        }
  339.     return pgs;
  340. }
  341.  
  342. /* Allocate the contents of a graphics state object. */
  343. /* Return -1 if the allocation fails. */
  344. /* Note that the contents have been smashed in this case. */
  345. private int
  346. alloc_gstate_contents(register gs_state *pgs)
  347. {    gs_proc_alloc_t palloc = pgs->memory_procs->alloc;
  348.     static const char cname[] = "alloc_gstate_contents";
  349.     gs_state_contents *cont =
  350.       (gs_state_contents *)(*palloc)(1, sizeof(gs_state_contents), cname);
  351.     if ( cont == 0 ) return -1;
  352. #define gset(element)\
  353.   pgs->element = &cont->element;
  354.     gset(path);
  355.     gset(clip_path);
  356.     gset(line_params);
  357.     gset(halftone);
  358.     gset(color_space);
  359.     cont->color_space.type = &gs_color_space_type_DeviceGray;    /* for cs_adjust_count */
  360.     gset(ccolor);
  361.     gset(dev_color);
  362. #undef gset
  363. #define galloc(element,type,fail)\
  364.   if ( (pgs->element = (type *)(*palloc)(1, sizeof(type), cname)) == 0 )\
  365.     goto fail
  366. #define gtalloc(element,fail)\
  367.   rc_allocate_struct(pgs->element, gx_transfer_map, pgs->memory_procs,\
  368.           goto fail, cname);
  369.     gtalloc(transfer.red, utr);
  370.     gtalloc(transfer.green, utg);
  371.     gtalloc(transfer.blue, utb);
  372.     gtalloc(transfer.gray, uty);
  373.     galloc(device, device, ud);
  374. #undef galloc
  375. #undef gtalloc
  376.     if ( pgs->client_data != 0 )
  377.     {    if ( (pgs->client_data =
  378.                (*pgs->client_procs.alloc)(pgs->memory_procs)) == 0
  379.            )
  380.             goto ucd;
  381.     }
  382.     rc_increment(pgs->cie_render);
  383.     rc_increment(pgs->cie_joint_caches);
  384.     pgs->device_is_shared = 0;
  385.     return 0;
  386.     /* Undo partial allocations if an allocation failed. */
  387. #define gunalloc(element) free_struct(pgs, pgs->element, cname)
  388. ucd:    gunalloc(device);
  389. ud:    gunalloc(transfer.gray);
  390. uty:    gunalloc(transfer.blue);
  391. utb:    gunalloc(transfer.green);
  392. utg:    gunalloc(transfer.red);
  393. utr:    free_struct(pgs, cont, cname);
  394.     return -1;
  395. #undef gunalloc
  396. }
  397.  
  398. /* Free the contents of a graphics state, but not the state itself. */
  399. private void
  400. free_gstate_contents(gs_state *pgs)
  401. {    gs_proc_free_t pfree = pgs->memory_procs->free;
  402.     static const char cname[] = "free_gstate_contents";
  403.     if ( pgs->client_data != 0 )
  404.         (*pgs->client_procs.free)(pgs->client_data, pgs->memory_procs);
  405. #define gfree(element)\
  406.     (*pfree)((char *)pgs->element, 1, sizeof(*pgs->element), cname)
  407.     gx_cpath_release(pgs->clip_path);
  408.     gx_path_release(pgs->path);
  409.     if ( !pgs->device_is_shared )
  410.         gfree(device);
  411.     rc_decrement(pgs->transfer.gray, pgs->memory_procs, cname);
  412.     rc_decrement(pgs->transfer.blue, pgs->memory_procs, cname);
  413.     rc_decrement(pgs->transfer.green, pgs->memory_procs, cname);
  414.     rc_decrement(pgs->transfer.red, pgs->memory_procs, cname);
  415.     rc_decrement(pgs->cie_render, pgs->memory_procs, cname);
  416.     rc_decrement(pgs->cie_joint_caches, pgs->memory_procs, cname);
  417.     cs_adjust_count(pgs, -1);
  418.     (*pfree)((char *)gstate_contents(pgs), 1, sizeof(gs_state_contents),
  419.          cname);
  420. #undef gfree
  421. }
  422.  
  423. /* Copy the composite parts of a graphics state. */
  424. private void
  425. copy_gstate_contents(gs_state *pto, const gs_state *pfrom)
  426. {    static const char cname[] = "copy_gstate_contents";
  427.     cs_adjust_count(pto, -1);
  428.     *gstate_contents(pto) = *gstate_contents(pfrom);
  429.     cs_adjust_count(pto, 1);
  430. #define gcopy(element)\
  431.     *pto->element = *pfrom->element
  432. #define rccopy(element)\
  433.     rc_assign(pto->element, pfrom->element, pto->memory_procs, cname);
  434.     rccopy(transfer.gray);
  435.     rccopy(transfer.blue);
  436.     rccopy(transfer.green);
  437.     rccopy(transfer.red);
  438.     rccopy(cie_render);
  439.     rccopy(cie_joint_caches);
  440.     gcopy(device);
  441. #undef gcopy
  442. #undef rccopy
  443.     if ( pfrom->client_data != 0 )
  444.         (*pfrom->client_procs.copy)(pto->client_data, pfrom->client_data);
  445.     gx_path_share(pto->path);
  446.     gx_cpath_share(pto->clip_path);
  447. }
  448.